package system

import (
	"context"
	"errors"
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type Seckill_activityService struct{}

// CreateSeckillActivityRequest 结构体和你 API 层一致
type CreateSeckillActivityRequest struct {
	Activity_name           string                           `json:"activity_name"`
	Activity_start_time     string                           `json:"activity_start_time"`
	Seckill_end_time        string                           `json:"seckill_end_time"`
	Seckill_activity_status string                           `json:"seckill_activity_status"`
	Devices                 []system.Seckill_activity_device `json:"devices"`
}

// CreateSeckill_activity 创建秒杀活动记录
// Author [yourname](https://github.com/yourname)
// CreateSeckill_activity 创建秒杀活动记录
func (seckill_activityService *Seckill_activityService) CreateSeckill_activity(ctx context.Context, req *CreateSeckillActivityRequest) (err error) {
	// 1. 活动查重
	var count int64
	global.GVA_DB.Model(&system.Seckill_activity{}).
		Where("activity_name = ?", req.Activity_name).
		Count(&count)
	if count > 0 {
		return errors.New("活动已存在")
	}

	// 2. 创建活动
	activity := system.Seckill_activity{
		Activity_name:           &req.Activity_name,
		Activity_start_time:     &req.Activity_start_time,
		Seckill_end_time:        &req.Seckill_end_time,
		Seckill_activity_status: &req.Seckill_activity_status,
	}
	if err := global.GVA_DB.Create(&activity).Error; err != nil {
		return fmt.Errorf("创建活动失败: %w", err)
	}
	// 3. 设备写入活动设备表
	activityID := int(activity.ID)
	for _, device := range req.Devices {
		// 复制设备数据，避免修改原始切片
		deviceCopy := device
		// 设置活动ID（使用副本避免引用问题）
		deviceCopy.Seckill_activity_id = &activityID

		if err := global.GVA_DB.Create(&deviceCopy).Error; err != nil {
			return fmt.Errorf("写入活动设备失败: %w", err)
		}
	}
	return nil
}

// DeleteSeckill_activity 删除秒杀活动记录
// Author [yourname](https://github.com/yourname)
func (seckill_activityService *Seckill_activityService) DeleteSeckill_activity(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.Seckill_activity{}, "id = ?", ID).Error
	return err
}

// DeleteSeckill_activityByIds 批量删除秒杀活动记录
// Author [yourname](https://github.com/yourname)
func (seckill_activityService *Seckill_activityService) DeleteSeckill_activityByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.Seckill_activity{}, "id in ?", IDs).Error
	return err
}

// UpdateSeckill_activity 更新秒杀活动记录
// Author [yourname](https://github.com/yourname)
func (seckill_activityService *Seckill_activityService) UpdateSeckill_activity(ctx context.Context, seckill_activity system.Seckill_activity) (err error) {
	err = global.GVA_DB.Model(&system.Seckill_activity{}).Where("id = ?", seckill_activity.ID).Updates(&seckill_activity).Error
	return err
}

// GetSeckill_activity 根据ID获取秒杀活动记录
// Author [yourname](https://github.com/yourname)
func (seckill_activityService *Seckill_activityService) GetSeckill_activity(ctx context.Context, ID string) (seckill_activity system.Seckill_activity, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&seckill_activity).Error
	return
}

// GetSeckill_activityInfoList 分页获取秒杀活动记录
// Author [yourname](https://github.com/yourname)
func (seckill_activityService *Seckill_activityService) GetSeckill_activityInfoList(ctx context.Context, info systemReq.Seckill_activitySearch) (list []system.Seckill_activity, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.Seckill_activity{})
	var seckill_activitys []system.Seckill_activity
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&seckill_activitys).Error
	return seckill_activitys, total, err
}
func (seckill_activityService *Seckill_activityService) GetSeckill_activityPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
